package com.example.park2301.news

import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.entity.NewsBeanItem
import com.example.lib_base.net.ApiService

class NewsDataSource(val apiService: ApiService) :PagingSource<Int ,NewsBeanItem>() {
    override fun getRefreshKey(state: PagingState<Int, NewsBeanItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, NewsBeanItem> {
       try {
           val startPage = params.key ?: 0
           val news = apiService.getNews()
           val nextPage = if (news.data.size==10) startPage+1 else null
           return LoadResult.Page(news.data,  null, nextPage)
       }catch (e:Exception){
           return LoadResult.Error(e)
       }
    }
}